package com.domdd.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domdd.dao.common.InventoryNewMapper;
import com.domdd.enums.upload.sos.CommonUpload;
import com.domdd.model.InventoryNew;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author David.Liu
 * @Date 2024-12-06 13:35
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class PageService {
    private final InventoryNewMapper inventoryNewMapper;

    public IPage<InventoryNew> inventoryNewList(Date inventoryDate, Integer page, Integer pageSize) {
        IPage<InventoryNew> p = new Page<>(page, pageSize);
        return inventoryNewMapper.selectByPageAndInventoryDate(p, inventoryDate);
    }

    public void exportExistCategoryLang(Date inventoryDate, HttpServletResponse response) throws IOException {
        List<InventoryNew> inventoryNewList = inventoryNewMapper.selectByInventoryDate(inventoryDate);
        List<List<Object>> dataList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(inventoryNewList)) {
            dataList.add(CollectionUtil.newArrayList(
                    "商品编码",
                    "规格条码",
                    "商品名称",
                    "规格名称",
                    "良品库存",
                    "残次品",
                    "残品",
                    "盒损",
                    "总库存"));
            for (InventoryNew inventoryNew : inventoryNewList) {
                dataList.add(CollectionUtil.newArrayList(
                        inventoryNew.getOuterId(),
                        inventoryNew.getBarcode(),
                        inventoryNew.getGoodsName(),
                        inventoryNew.getSkuName(),
                        inventoryNew.getNormalQuantity(),
                        inventoryNew.getSubstandardQuantity(),
                        inventoryNew.getBrokenQuantity(),
                        inventoryNew.getBoxBrokenQuantity(),
                        inventoryNew.getQuantity()
                ));
            }
        }

        String filePre = "库存详情_" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
        CommonUpload.exportToXlsx(response, "库存详情", filePre, dataList);
    }
}
